package com.ld.common.util.filelistener.commonio.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import com.ld.admin.model.system.NoticeActionModel;
import com.ld.common.util.filelistener.FileChangeModel;

import lombok.extern.slf4j.Slf4j;

@Slf4j
/**
 * 文件变化处理器（默认实现，只打印了信息，需要重新继承覆盖对应的方法，本类一般不直接使用，覆盖具体的方法后使用）
 * 
 * @ClassName FileChangedListener
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年1月17日 下午5:04:56
 *
 */
public class AbstractChangedHandler extends FileAlterationListenerAdaptor {
    protected NoticeActionModel noticActionModel = null; // 通知动作配置
    protected List<FileChangeModel> changedFiles = new ArrayList<>(); // 变化文件
    protected Integer lastChangedSize = 0; // 上次变化文件数，用于比较文件是否又发生了变化

    public AbstractChangedHandler() {
        super();
    }

    public AbstractChangedHandler(NoticeActionModel noticActionModel) {
        super();
        this.noticActionModel = noticActionModel;
    }

    /**
     * File system observer started checking event.
     */
    @Override
    public void onStart(FileAlterationObserver observer) {
        super.onStart(observer);
        log.debug("文件系统观察者开始检查事件");
    }

    /**
     * File system observer finished checking event.
     */
    @Override
    public void onStop(FileAlterationObserver observer) {
        super.onStop(observer);
        log.debug("文件系统完成检查事件观测器");
    }

    /**
     * Directory created Event.
     */
    @Override
    public void onDirectoryCreate(File directory) {
        super.onDirectoryCreate(directory);
        log.warn("目录创建事件" + directory.getAbsolutePath());
    }

    /**
     * Directory changed Event
     */
    @Override
    public void onDirectoryChange(File directory) {
        super.onDirectoryChange(directory);
        log.warn("目录改变事件" + directory.getAbsolutePath());
    }

    /**
     * Directory deleted Event.
     */
    @Override
    public void onDirectoryDelete(File directory) {
        super.onDirectoryDelete(directory);
        log.warn("目录删除事件" + directory.getAbsolutePath());
    }

    /**
     * File created Event.
     */
    @Override
    public void onFileCreate(File file) {
        super.onFileCreate(file);
        log.warn("文件创建事件");
        log.warn("文件：" + file.getAbsolutePath());
    }

    /**
     * File changed Event.
     */
    @Override
    public void onFileChange(File file) {
        super.onFileChange(file);
        log.warn("文件改变事件" + file.getAbsolutePath());
    }

    /**
     * File deleted Event.
     */
    @Override
    public void onFileDelete(File file) {
        super.onFileDelete(file);
        log.warn("文件删除事件:" + file.getAbsolutePath());
    }

}
